package shuzi.adapter;

import java.util.List;

import shuzi.data.Database;
import shuzi.model.Brand;
import shuzi.widget.BrandImageView;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.shuzi.offer.R;
import com.shuzi.offer.R.color;

public class BrandAdapter extends BaseAdapter {

	private final LayoutInflater mInflater;
	private List<Brand> list;
	private Context context;
	private DisplayImageOptions options;
	private ImageLoader imageLoader = null;
	

	public BrandAdapter(Context context, List<Brand> list) {
		this.mInflater = LayoutInflater.from(context);
		this.list = list;
		this.context = context;
		init();
	}

	private void init(){
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
		.showStubImage(R.drawable.imgd)
		.showImageForEmptyUri(R.drawable.imgd)
		.showImageOnFail(R.drawable.imgd)
		.cacheInMemory()
		.cacheOnDisc()
		//.displayer(new RoundedBitmapDisplayer(10))
		.build();
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return this.list.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return this.list.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public final class ViewHolder {
		public BrandImageView img;
		private final View baseView;

		public ViewHolder(View baseView) {
			this.baseView = baseView;
		}

		public BrandImageView getImg() {
			return (BrandImageView) baseView.findViewById(R.id.brandImage);
		}

		public void setImg(BrandImageView img) {
			this.img = img;
		}

	}
	
	

	public View getView(int position, View convertView, ViewGroup parent) {

	//	Log.v(Database.TAG, position+"");
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = (RelativeLayout) this.mInflater.inflate(
					R.layout.brand_item, null);
			holder = new ViewHolder(convertView);
			holder.img = holder.getImg();

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();

		}
		 holder.img.setImageDrawable(new ColorDrawable(color.lightgreen));
		 holder.img.setScaleType(ScaleType.FIT_XY);
		// holder.img.setImageViewBg(list.get(position).getBrandTypeMlogo(), list.get(position).getBrandTypeName(),position,Database.BRAND_INFO);
		 imageLoader.displayImage(list.get(position).getBrandTypeMlogo(), holder.img, options);
		// FadeInBitmapDisplayer.animate(holder.img,100);
		return convertView;
	}

}
